<template>
	<view v-show="display">
		<view class="">
			<u-swiper :height="563" :list="list1"></u-swiper>
		</view>
		<view class="content">
			<view class="content_1">
				<text class="content_1_title">{{contentlist.title}}
				</text>
				<view class="content_1_label">
					<!-- <text>1人份</text>
					<text>咸鲜</text> -->
				</view>
				<view class="content_1_sales">
					月售{{contentlist.count}}+
				</view>
				<view class="content_1_price">
					<view class="">
						¥{{contentlist.minprice}}
					</view>
					<view class="" @tap="select" v-if="contentlist.sku_count>0">
						选规格
					</view>
					<view class="" @click="add_joinCar(contentlist.id)" v-else>
						加入购物车
					</view>
				</view>
			</view>
			<view class="content_2">
				<view class="content_2_title">
					详情
				</view>
				<view class="content_2_text" v-html="contentlist.json">
				</view>
				<height :hg='80'></height>
			</view>
		</view>
		<view class="settle">
			<close :shopping_trolley_list='shopping_trolley_list' @update='order_carListApi' @register='reg'
				:addAdish='addAdish' :seatShow='seatShow'></close>
		</view>
		<specification ref='specification' :store_id='store_id' :activate_data='activate_data' :order_id='order_id'
			:type='order_type' @order='order_carListApi' :show='show_specification' @close='show_specification=false'
			@login='enter=true'>
		</specification>

		<login :show="enter" @closepage='enter=false'></login>
	</view>
</template>

<script>
	import {
		store_goodsDetail,
		store_item,
		store_itemPrice,
		order_joinCar,
		order_carList,
	} from "@/api/comm.js"
	import Location from "@/utils/wxApi.js"
	export default {
		data() {
			return {
				enter: false,
				addAdish: false,
				list1: [],
				store_id: '',
				orderid: '', //商品id
				contentlist: {}, //页面数据
				// 规格参数的弹出层
				show_specification: false,
				specification_list: [], //规格参数
				order_id: '', //当前规格商品id
				style: [], //规格动态样式
				orderprive: 0, //规格价格计算
				shopping_trolley_list: {}, //购物车列表
				display: false,
				register: false,
				activate_data: '',
				seatShow: true,
				addAdish: false,
				order_type: ""
			};
		},
		onLoad(obj) {
			this.order_id = obj.id
			this.store_id = obj.store_id
			this.activate_data = obj.activate_data
			this.store_goodsDetailApi(obj.id)
			this.store_itemApi(obj.id)

			let add = uni.getStorageSync('addAdish') //加菜还是结算
			let option = uni.getStorageSync('option')
			if (option == 3) {
				this.seatShow = false
				if (add) {
					this.addAdish = add
				}
			}
			if (uni.getStorageSync('userinfo')) {
				this.order_carListApi()
			}
		},
		methods: {
			// 添加购物车
			async store_goodsDetailApi(id) {
				let data = await store_goodsDetail({
					goods_id: id
				})
				if (data.code == 1) {
					this.list1 = []
					this.list1.push(...data.data.images)
					this.contentlist = data.data
					this.display = true
					this.order_type = data.data.type
					uni.setNavigationBarTitle({
						title: this.contentlist.title //这是修改后的导航栏文字
					})
				}

			},
			async store_itemApi(id) {
				let data = await store_item({
					goods_id: id
				})
				data.data.specs.forEach(res => {
					this.style.push({
						id: res.item[0].id,
						name: res.item[0].name
					})
				})
				this.specification_list = []
				this.specification_list.push(...data.data.specs)
				this.store_itemPriceApi()
			},

			async selective_specification(index, id, name) {
				this.$set(this.style[index], 'id', id)
				this.$set(this.style[index], 'name', name)
				this.store_itemPriceApi()
			},
			// 价格计算
			async store_itemPriceApi() {
				let str = ''
				this.style.forEach(res => {
					str += res.id + '_'
				})
				str = str.substring(0, str.length - 1)
				let data = await store_itemPrice({
					goods_id: this.order_id,
					spu_id: str
				})
				this.orderprive = Location.BumberPrecision(data.data.price)
			},
			// 添加购物车
			async add_joinCar() {
				if (!uni.getStorageSync('userinfo')) {
					this.enter = true
					return
				}
				let str = ''
				this.style.forEach(res => {
					str += res.id + '_'
				})
				str = str.substring(0, str.length - 1)
				let data = await order_joinCar({
					store_id: this.store_id,
					goods_id: this.order_id,
					spu_id: str,
					count: 1,
					order_type: this.activate_data
				})
				if (data.code == 1) {
					uni.showToast({
						title: data.msg,
						icon: "success"
					})
					this.order_carListApi()
					this.show_specification = false
				} else {
					uni.showToast({
						title: data.msg,
						icon: "error"
					})
				}
			},
			// 购物车列表
			async order_carListApi() {
				let peopleCount = uni.getStorageSync('peopleCount') || 0
				let type;
				if (this.addAdish) {
					type = 2
				} else {
					type = 1
				}
				let data = await order_carList({
					peopleCount: peopleCount,
					type
				})

				this.shopping_trolley_list = data.data
			},
			select() {
				setTimeout(() => {
					this.$refs.specification.specification()
					this.show_specification = true
				}, 500)
			},
			reg() {
				if (!uni.getStorageSync('userinfo')) {
					this.register = true
					uni.showToast({
						title: '未登录',
						icon: 'none'
					})
					return
				}
				this.show_specification = true
			},
			closepage() {
				this.register = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content_1 {
		background-color: #FFFFFF;
		padding: 30rpx;

		.content_1_title {
			font-size: 32rpx;
			font-weight: 500;
			color: #353535;
		}

		.content_1_label {
			margin-top: 20rpx;

			text {
				background-color: #F5F5F5;
				font-size: 24rpx;
				font-weight: 400;
				color: #353535;
				padding: 5rpx 10rpx;
				margin-right: 10rpx;
			}
		}

		.content_1_sales {
			font-size: 24rpx;
			font-weight: 400;
			color: #8A8A8A;
			margin-top: 20rpx;
		}

		.content_1_price {
			display: flex;
			align-items: center;
			justify-content: space-between;

			view:nth-child(1) {
				font-size: 34rpx;
				font-weight: 600;
				color: #FC4F46;
			}

			view:nth-child(2) {
				width: 209rpx;
				height: 61rpx;
				background: #6F3810;
				border-radius: 13rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 61rpx;
				text-align: center;
				margin-top: 20rpx;
			}
		}
	}

	.content_2 {
		background: #FFFFFF;
		margin-top: 30rpx;
		padding: 30rpx 0;

		.content_2_title {
			font-size: 32rpx;
			font-weight: 600;
			color: #353535;
			padding: 0 30rpx;
			padding-bottom: 30rpx;
			border-bottom: 1rpx solid #ECECEC;
		}

		.content_2_text {
			min-height: 300rpx;
			height: auto !important;
			padding: 20rpx 20rpx;
		}
	}

	.settle {
		position: fixed;
		bottom: 50rpx;
	}

	.specification {
		width: 688rpx;
		background: #FFFFFF;
		border-radius: 12rpx;
		position: relative;
		z-index: 10074;

		.specification_title {
			text-align: center;
			font-size: 32rpx;
			font-weight: 600;
			color: #353535;
			margin: 0 auto;
		}

		.specification_title_1 {
			width: 95%;
			height: 504rpx;
			margin: 0 auto;

			.specification_title_1_title {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #676767;
				display: inline-block;
				padding: 20rpx 0;

			}

			.specification_title_1_content {
				display: flex;
				align-items: center;
				overflow-x: auto;
				display: -webkit-box;

				.specification_title_1_content_flex_activate {
					background: #F4FCFF !important;
					border: 1rpx solid #6F3810 !important;
					color: #6F3810 !important;
				}

				.font_sizi_1 {
					color: #6F3810;
				}

				.font_sizi_2 {
					border-left: 1rpx solid #6F3810;
				}

				.specification_title_1_content_flex {
					height: 63rpx;
					background: #FFFFFF;
					border-radius: 14rpx;
					border: 1rpx solid #F1F1F1;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-right: 20rpx;

					view:nth-child(1) {
						font-size: 24rpx;
						font-weight: 400;
						color: #343434;
						line-height: 63rpx;
						text-align: center;
						padding: 0 10rpx;
					}

					view:nth-child(2) {
						font-size: 24rpx;
						font-weight: 600;
						color: #F65329;
						line-height: 63rpx;
						border-left: 1rpx solid #F5F5F5;
						text-align: center;
						padding: 0 10rpx;
					}
				}
			}
		}

		.close {
			position: absolute;
			bottom: -150rpx;
			left: 50%;
			transform: translateX(-50%);
		}
	}

	.selected {
		width: 688rpx;
		padding: 20rpx 0;
		background: #F5F5F5;
		margin-top: 60rpx;

		text {
			font-size: 24rpx;
			font-weight: 400;
			color: #363636;
		}

		text:nth-child(1) {
			font-size: 24rpx;
			font-weight: 400;
			color: #676767;
			padding: 0 20rpx;
			margin-left: 10rpx;
		}
	}

	.sublist {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 40rpx;

		.sublist_left {
			text:nth-child(1) {
				font-size: 28rpx;
				font-weight: 600;
				color: #363636;
			}

			text:nth-child(2) {
				font-size: 28rpx;
				font-weight: 600;
				color: #FF4000;

				text:nth-child(1) {
					font-size: 28rpx;
					font-weight: 600;
					color: #FF4000;
					font-size: 24rpx;
				}
			}
		}

		.sublist_right {
			width: 234rpx;
			height: 62rpx;
			background: #6F3810;
			border-radius: 12rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #F3FCFF;
			line-height: 62rpx;
			text-align: center;
		}
	}

	.coupn {
		position: absolute;
		bottom: 150rpx;
		right: 0;
		z-index: 999;

		.coupn_icon {
			width: 24rpx;
			height: 24rpx;
			margin-left: 100rpx;
			margin-bottom: 10rpx;
			opacity: 0.5;
		}

		.coupn_img {
			width: 130rpx;
			height: 121rpx;
		}

		.coupn_title {
			padding: 5rpx 10rpx;
			background: #FEDD5B;
			border-radius: 18rpx;
			display: flex;
			align-items: center;
			position: relative;
			bottom: 30rpx;

			.coupn_title_text {
				font-size: 22rpx;
				font-weight: 400;
				color: #631407;
				margin-left: 10rpx;
			}
		}
	}
</style>